package org.hncors.gnssparse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.hncors.gnssparse.R;
import org.hncors.gnssparse.model.Message;

import java.util.List;


/**
 * Created by YAGUI on 2016/11/28.
 */

public class MsgListAdapter extends BaseAdapter {
    private static String TAG = "MsgListAdapter";


    private Context mContext;
    //xml转View对象
    private LayoutInflater mInflater;
    //单行的布局
    private int mResource;
    //列表展现的数据
    private List<Message> mList;

    public MsgListAdapter(Context context, List<Message> List){
        this.mContext = context;
        this.mList = List;
    }
    @Override
    public int getCount(){
    if (mList == null) {
        return 0;
    }
    return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
         if(convertView == null){
        holder = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.msg_item, null, false);

        //使用减少findView的次数

        holder.itemTime = ((TextView) convertView.findViewById(R.id.item_time));
        holder.itemTitle = ((TextView) convertView.findViewById(R.id.item_title));
        holder.itemText = ((TextView) convertView.findViewById(R.id.item_text));
        //设置标记
        convertView.setTag(holder);
        }else{
          holder = (ViewHolder) convertView.getTag();
        }

        //获取该行数据
        Message item = mList.get(position);

        holder.itemTime.setText(item.getTime());
        holder.itemTitle.setText(item.getTitle());
        holder.itemText.setText(item.getContent());

        return convertView;
    }
    /**
     * ViewHolder类
     */
    static class ViewHolder {
        TextView itemText;
        TextView itemTitle;
        TextView itemTime;
    }
}
